#include "pilecomplexes.h"

void pileComplexes::addX(const QString c){
    if(c == "$"){
        if(im == false){
            im = true;
            dot = false;
            decimal = 0.1;
            precision = 0;
            pA->setText(getQStringX());
        }
    }
    else if(c == ","){
        if(dot == false){
            dot = true;
            pA->setText(getQStringX());
        }
    }
    else if(c == "-" && im == false){
        negatifRe = !negatifRe;
        pA->setText(getQStringX());
    }
    else if(c == "-" && im == true){
        negatifIm = !negatifIm;
        pA->setText(getQStringX());
    }
    else{
        if (dot == false && im == false && (x.real()*10+c.toInt()) < MAX){
            x.setReal(x.real()*10+c.toInt());
            x.setP_Re(++ precision);
        }
        else if(dot == false && im == true &&(x.imag()*10+c.toInt()) < MAX){
            x.setImag(x.imag()*10+c.toInt());
            x.setP_Im(++ precision);
        }
        else if (dot == true && im == false && decimal > MAX_DECIMAL){
            x.setReal(x.real()+c.toInt()*decimal);
            decimal *= 0.1;
            x.setP_Re(++ precision);
        }
        else if(dot == true && im == true && decimal > MAX_DECIMAL){
            x.setImag(x.imag()+c.toInt()*decimal);
            decimal *= 0.1;
            x.setP_Im(++ precision);
        }
        pA->setText(getQStringX());
    }
}

void pileComplexes::iniX(){
    MAX_DECIMAL = 0.00000001;
    im = false;
    dot = false;
    negatifRe = false;
    negatifIm = false;
    decimal = 0.1;
    precision = 0;
    x = complexe(0,0);
    updateAfficheur();
}

complexe pileComplexes::getX() const{
    if (!negatifRe && !negatifIm) return x;
    else if (negatifRe && !negatifIm) return complexe(-x.real(),x.imag());
    else if (!negatifRe && negatifIm) return complexe(x.real(),-x.imag());
    else return complexe(-x.real(),-x.imag());
}

QString pileComplexes::getQStringX()const{
    return getX().toQString();
}

void pileComplexes::updateAfficheur()const{
    // met a jour l'afficheur pour la pile
    QString h = "";
    QStack<complexe>::const_iterator i;
    for (i = begin(); i != end(); ++i)
        h += i->toQString() + "\n";
    aPile->setText(h);
}

void pileComplexes::back(){
    if (dot == false){
        if(im){
            x.setImag(floor(x.imag()/10));
            precision --;
        }
        else{
            x.setReal(floor(x.real()/10));
            precision --;
        }
    }
    pA->setText(getQStringX());
}

 void pileComplexes::sauvegarder()const{
     QSettings settings("mesOptions.ini", QSettings::IniFormat);
     int taille = this->size();
     settings.beginGroup("PileComplexes");
     settings.setValue("taille", taille);
     settings.beginWriteArray("Valeur");
     const complexe *data = this->data();
     for(int i=0; i!=taille; i++){
        settings.setArrayIndex(i);
        settings.setValue("Complexe", data[i].toQString());
    }
     settings.endArray();
     settings.endGroup();
 }
